//package com.yyks.gateway.config;
//
//import com.alibaba.fastjson.JSONObject;
//import com.alibaba.nacos.api.NacosFactory;
//import com.alibaba.nacos.api.config.ConfigService;
//import com.alibaba.nacos.api.config.listener.Listener;
//import com.alibaba.nacos.api.exception.NacosException;
//import com.google.common.collect.Lists;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
//import org.springframework.cloud.gateway.route.RouteDefinition;
//import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
//import org.springframework.context.ApplicationEventPublisher;
//import org.springframework.context.ApplicationEventPublisherAware;
//import org.springframework.stereotype.Component;
//import reactor.core.publisher.Mono;
//
//import javax.annotation.PostConstruct;
//import java.util.List;
//import java.util.Properties;
//import java.util.concurrent.Executor;
//
///**
// * 实现路由动态加载配置文件
// */
//
////@Component
//@Slf4j
//public class NacosDynamicRouteService implements ApplicationEventPublisherAware {
//
//    @Value("${nacos.config.data-id}")
//    private String dataId;
//
//    @Value("${nacos.config.group}")
//    private String group;
//
//    @Value("${spring.cloud.nacos.config.server-addr}")
//    private String serverAddr;
//    @Value("${spring.cloud.nacos.config.namespace}")
//    private String namespace;
//
//    @Autowired
//    private RouteDefinitionWriter routeDefinitionWriter;
//
//    private ApplicationEventPublisher applicationEventPublisher;
//
//    private static final List<String> ROUTE_LIST = Lists.newArrayList();
//
//
//    //监听配置文件事件
//
//    @PostConstruct
//    public void dynamicRouteByNacosListener() {
//
//        try {
//            Properties properties = new Properties();
//            properties.setProperty("serverAddr", serverAddr);
//            properties.setProperty("namespace", namespace);
//            ConfigService configService = NacosFactory.createConfigService(properties);
//            log.info("配置文件地址：" + serverAddr);
//            log.info("命名空间为：" + namespace);
//            configService.getConfig(dataId, group, 5000);
//            configService.addListener(dataId, group, new Listener() {
//
//                @Override
//                public Executor getExecutor() {
//                    return null;
//                }
//
//                @Override
//                public void receiveConfigInfo(String configInfo) {
//                    clearRoute();
//                    List<RouteDefinition> routeDefinitionList = JSONObject.parseArray(configInfo, RouteDefinition.class);
//                    routeDefinitionList.forEach(routeDefinition -> {
//                        log.info("==========监听的相关配置信息：=========" + routeDefinition);
//                        addRoute(routeDefinition);
//                    });
//                    publish();
//                }
//            });
//
//        } catch (NacosException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 清空路由信息
//     */
//    private void clearRoute() {
//        ROUTE_LIST.forEach(id -> {
//            this.routeDefinitionWriter.delete(Mono.just(id)).subscribe();
//        });
//        ROUTE_LIST.clear();
//    }
//
//    /**
//     * 新增路由信息
//     */
//    private void addRoute(RouteDefinition definition) {
//        try {
//            routeDefinitionWriter.save(Mono.just(definition)).subscribe();
//            ROUTE_LIST.add(definition.getId());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 添加事件
//     */
//    public void publish() {
//        this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this.routeDefinitionWriter));
//    }
//
//    @Override
//    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
//        this.applicationEventPublisher = applicationEventPublisher;
//    }
//}
